package com.weaver.drools.common.utils;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
/**
 * 
 *  @application name:分页util
 *  @author: zhouxinlei 
 *  @time：2018年8月13日
 *  @version：ver 1.1
 */
public class PageTag<T> implements Serializable{
	private static final long serialVersionUID = 1L;
	/** 页码 */
	protected int pageNo = 1;
	/** 每页数据 */
	protected int pageSize = 10;
 
	/** 返回结果 */
	protected List<T> result = Collections.emptyList();
 
	/** 数据总数 */
	protected long totalCount = -1;
 
 
	public PageTag() {
		super();
	}

	public PageTag(int pageNo, int pageSize, List<T> result, long totalCount) {
		super();
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.result = result;
		this.totalCount = totalCount;
	}

	/**
	 * 获得当前页的页号,序号从1开始,默认为1.
	 */
	public int getPageNo() {
		return pageNo;
	}
 
	/**
	 * 设置当前页的页号,序号从1开始,低于1时自动调整为1.
	 */
	public void setPageNo(final int pageNo) {
		this.pageNo = pageNo;
 
		if (pageNo < 1) {
			this.pageNo = 1;
		}
	}
 
	public PageTag<T> pageNo(final int thePageNo) {
		setPageNo(thePageNo);
		return this;
	}
 
	/**
	 * 获得每页的记录数量
	 */
	public int getPageSize() {
		return pageSize;
	}
 
	/**
	 * 设置每页的记录数量,低于1时一页显示全部记录.
	 */
	public void setPageSize(final int pageSize) {
		this.pageSize = pageSize;
	}
 
	public PageTag<T> pageSize(final int thePageSize) {
		setPageSize(thePageSize);
		return this;
	}
 
	public List<T> getResult() {
		return result;
	}
 
	public void setResult(List<T> result) {
		this.result = result;
	}
 
	public PageTag<T> result(List<T> result) {
		setResult(result);
		return this;
	}
 
	public long getTotalCount() {
		return totalCount;
	}
 
	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}
 
	public PageTag<T> totalCount(long totalCount) {
		setTotalCount(totalCount);
		return this;
	}
 
	/**
	 * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
	 */
	public int getFirst() {
		return (this.totalCount == 0) ? 0 : ((pageNo - 1) * pageSize) + 1;
	}
 
 
	public long getTotalPages() {
		if (totalCount < 0)
			return 0;
 
		long count = totalCount / pageSize;
		if (totalCount % pageSize > 0) {
			count++;
		}
		return count;
	}
	
}
